<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function loginAction(){

        $username=I('post.username');
        $password=I('post.password');

        $strUserName = trim(I('post.username'));
        $intReplyId = max(0,intval(I('get.id')));

        if(!empty($username) && !empty($password)){

            $rem=I('post.rem');
            $pwd=md5($password);


            $where = Array(
                'username'=>$username,
                'password'=>$pwd,
            );


            $user = M("user"); // 实例化对象
            $arr = $user->where($where)->find();//查询
           //echo $user->getlastsql();exit;
           //print_r($arr);exit;
            if($arr){
                //session_start();
                session('user',$arr);  //设置session
                if(!empty($rem)){
                    cookie('username',"$username",3600*24); // 指定cookie保存时间
                    cookie('password',"$password",3600*24); // 指定cookie保存时间
                }else{
                    cookie('username',null);
                    cookie('password',null);
                }
                $this->success('登录成功!','index.php/home/liuyan/list/p/1');exit;
            }else{
                $this->error('登录失败!','index.php');exit;
            }
        }
        $act='用户登录';
        $this->assign('act',$act);// 赋值
        $this->display('login');//加载登录视图
    }

    /**
     *
     */
    public function adminAction(){
        $username=I('post.username');
        $password=I('post.password');
        if(!empty($username) and !empty($password)){
            $rem=I('post.rem');
            $pwd=md5($password);
            $where="`username`='$username' and `password`='$pwd'";
            $admin = M("admin"); // 实例化对象
            $arr = $admin->where($where)->find();//查询
            //echo $user->getlastsql();exit;
            //print_r($arr);exit;
            if($arr){
                //session_start();
                session('admin',$arr);  //设置session
                if(!empty($rem)){
                    cookie('adminname',"$username",3600*24); // 指定cookie保存时间
                    cookie('adminpwd',"$password",3600*24); // 指定cookie保存时间
                }else{
                    cookie('adminname',null);
                    cookie('adminpwd',null);
                }
                $this->redirect('home/liuyan/list/p/1','', 3, '登录成功！等待跳转...');exit;
            }else{
                $this->redirect('','', 3, '登录失败！等待跳转...');exit;
            }
        }
        $act='管理员登录';
        $this->assign('act',$act);// 赋值
        $this->display('login');//加载登录视图
    }
    public function zhuceAction(){
        $add=I('post.');

        if(!empty($add)){
            if($add['password']=$add['pwd']){unset($add['pwd']);}else{
                $this->redirect('','', 3, '密码不一致！等待跳转...');exit;
            }
            $add['password'] = md5($add['password']);
            $add['ip'] = get_client_ip();//ip
            //print_r($add);exit;
            $user = M("user"); // 实例化对象
            $result=$user->data($add)->add();

            if($result>0){
                $this->redirect('','', 3, '注册成功！请登录！等待跳转...');exit;
            }else{
                $this->redirect('','', 3, '注册失败！等待跳转...');exit;
            }
        }
        $this->display('zhuce');//加载视图
    }
}